import { delAddress } from '@/common/api/users'
import { mapGetters } from 'vuex'
const delAddr = {
	computed: {
		...mapGetters([
			'userInfo',
			'shop',
			'address'
		])
	},
	data () {
		return {
			params: {
				uname: '',
				ticket: '',
				user_id:'',
				customer_id: ''
			}			
		}
	},
	created () {
		if (this.edit) this.getParams()
	},
	methods: {
		delAddress(id) {
			uni.showModal({
				title: '',
				content: '确认删除该地址？',
				success: (rst) => {
					if (rst.confirm) {
						delAddress(Object.assign({}, { id }, this.params)).then(res => {
							if (res.status) {
								this.$store.commit('SET_ADDRESS', this.address.filter(item => item.id !== id))
								if (!this.address.length && !this.edit) this.noData = true
								if (this.edit) {uni.navigateBack()} else { uni.showToast({ title: '删除成功' }) }
							}
						})
					}
				}
			})
		},
		getParams () {
			if (!this.shop) {
			   setTimeout(this.getList, 500)
			   return
			}
			this.params.uname = this.userInfo.uname
			this.params.ticket = this.userInfo.ticket
			this.params.user_id = this.userInfo.id
			this.params.customer_id = this.shop.customer_id				
		}
	}
}

export default delAddr